<script type="text/javascript">
/*----------------------------------------------------------
 EVENT
-----------------------------------------------------------*/
//create
$(document).on("click", "#create-type", function(){
    var url = "<?php echo Yii::app()->createUrl('dadmin/newsletterType/create/parentId'); ?>/"
        + $('#parentId').val();
    loadFrame(url, '#dialogForm', 280, refreshMenu, 'frameId1', '<?php echo Yii::t('DadminModule.app', 'add') ?>');
});

//update
$(document).on("click", ".updateType", function(){
    var url = $(this).attr('href');
    loadFrame(url, '#dialogForm', 280, refreshMenu, 'frameId1', '<?php echo Yii::t('DadminModule.app', 'update') ?>');

    return false;
});

//delete
$(document).on("click", ".deleteType", function(){
    var params = { msg: '<?php echo Yii::t('DadminModule.msg', 'delete_confirm') ?>' };
    var url = $(this).attr('href');
    
    $('#null').showConfirm(params, function(){
        $.getJSON(url, '', function(result) {
            if ( result.success === true ) {
                refreshMenu();
                showMessage('#msgSuccess', result.message);
            } else {
                showMessage('#msgError', result.message);
            }
        });
    }, '');
    
    return false;
});
/*----------------------------------------------------------
PROCESSING
 ----------------------------------------------------------*/
function loadFrame(url, container, height, callBack, frameId, title) {
    var frameAttr = 'width="100%" height="100%"'
        + 'marginWidth="0" marginHeight="0"'
        + 'frameBorder="0"';

    $(container).html('<iframe id="' + frameId + '" '+ frameAttr + '/>').dialog({
        modal: true,
        width: 500, height: height,
        position: ['center', 55],
        draggable: false, resizable: false,
        bgiframe: true,
        close: callBack,
        title: title,
        zIndex: 10000
    });
    $("#" + frameId).attr("src", url);

    return false;
};

function showConfirm() {
    return confirm("<?php echo Yii::t('DadminModule.msg', 'delete_confirm') ?>");
}

function refreshMenu() {
    loadMenu();
    loadChildMenu();
}

function loadMenu() {
    var url = "<?php echo $this->createUrl('newsletterType/menuShowFull'); ?>";
    $('#typeTree').load(url);
}

function loadChildMenu() {
    var parentId = $('#parentId').val();
    $('#typeTree .link').each(function(){
        if ($(this).attr('ref') == parentId)
            $(this).trigger('click');
    })
}
</script>